//
//  BVPopoverBaseView.m
//  BVHomeScene
//
//  Created by xiute on 2019/10/28.
//




#import "BVPopoverBaseView.h"


@interface BVTriangleView : UIView

@end


@interface BVPopoverBaseView ()

@property (nonatomic, strong) BVTriangleView *triangleView;

@end

@interface BVPopoverBaseView ()

@property (nonatomic) CGPoint point;

@end

@implementation BVPopoverBaseView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

+ (instancetype)showInView:(UIView *)aView point:(CGPoint)point{
    BVPopoverBaseView *view = [[self alloc] initWithFrame:aView.bounds];
    view.point = point;
    [aView addSubview:view];
    [view setupViews];
    return view;
}

- (void)setupViews {
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    self.triangleView.center = self.point;
    self.triangleView.top = self.point.y;
    [self addSubview:self.triangleView];
    
    self.contentView.frame = CGRectMake(50, self.triangleView.bottom, self.width - 50 - 10, 306);
    [self addSubview:self.contentView];
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 8;
        _contentView.clipsToBounds = YES;
    }
    return _contentView;
}

- (BVTriangleView *)triangleView {
    if (!_triangleView) {
        _triangleView = [[BVTriangleView alloc] initWithFrame:CGRectMake(0, 0, 15, 7)];
        _triangleView.backgroundColor = [UIColor clearColor];
    }
    return _triangleView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self removeFromSuperview];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}


@end






@implementation BVTriangleView

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, self.width / 2.0, 0);
    CGPathAddLineToPoint(path, nil, 0, self.height);
    CGPathAddLineToPoint(path, nil, self.width, self.height);
    CGPathAddLineToPoint(path, nil, self.width / 2.0, 0);
    CGContextAddPath(context, path);
    
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 1);
    CGContextDrawPath(context, kCGPathFill);
    CGPathRelease(path);
    
}

@end
